<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <button class="btn">计算</button>
    <input type="text" placeholder="请输入一些内容" />
    <script type="module">
      import { add } from "./math.js";
      /*
        斐波那契数列
          0 1 1 2 3 5 8 13 21 34 55 ...

          f(1)  0
          f(2)  1
          f(3)  1 = f(1) + f(2)
          f(4)  2 = f(2) + f(3)
          f(5)  3 = f(3) + f(4)


          f(50) 
            f(48)             f(49)
              f(46) f(47)      f(48) f(46)
      */

      // function fibonacci(n) {
      //   if (n === 1) return 0;
      //   if (n === 2) return 1;
      //   return fibonacci(n - 2) + fibonacci(n - 1);
      // }

      document.querySelector(".btn").onclick = function () {
        // console.log(fibonacci(50));
        // 点击按钮开启分线程，通知它开始计算
        // const worker = new Worker("./module-worker.js");
        const worker = new Worker("./module-worker.js", {
          type: "module",
        });
        // 主线程和分线程之间如何通信
        // 主线程发送数据给分线程 postMessage
        worker.postMessage("主线程通知分线程干活啦~");
        // 主线程接受分线程发送过来的数据 message事件
        worker.onmessage = function (e) {
          console.log(e.data);
        };
      };
    </script>
  </body>
</html>
